<template>
  <div class="departments">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="组织架构" name="second">
          <div class="main">
            <Treeltem :item="topItme" />
            <hr>
            <!-- 树形控件 -->
            <el-tree :data="treeDate" :props="{ label: 'name' }">
              <template v-slot="{ data }">
                <Treeltem style="width: 100%" :item="data" @getDate="getDate" />
                <!-- <div class="top" style="width: 100%">
                  <div class="title">{{ data.name }}</div>
                  <div class="user">{{ data.nagename }}</div>
                  <el-dropdown>
                    <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right" />
                    </span>
                    <template #dropdown>
                      <el-dropdown-menu>
                        <el-dropdown-item>添加子部门</el-dropdown-item>
                        <el-dropdown-item>查看部门</el-dropdown-item>
                        <el-dropdown-item>删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </template>
                  </el-dropdown>
                </div> -->
              </template>
            </el-tree>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹框 -->
    <Add :init-date="initDate" @getDate="getDate" />
  </div>
</template>

<script>
import Treeltem from './components/treeltem.vue'
import Add from './components/add.vue'
import { companyDepartment } from '@/api/department.js'

export default {
  components: {
    Treeltem,
    Add
  },
  data() {
    return {
      activeName: 'second',
      treeDate: [],
      initDate: [],
      topItme: {
        id: '',
        name: '',
        manager: '负责人',
        istree: true
      }
    }
  },
  created() {
    this.getDate()
  },
  methods: {
    async getDate() {
      const res = await companyDepartment()
      this.topItme.name = res.data.companyName
      this.initDate = res.data.depts
      this.treeDate = this.changeDate(res.data.depts, '')
      console.log(res)
    },
    changeDate(arr, pid) {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeDate(arr, item.id)
          return true
        }
      })
    }
  }
}
</script>

<style lang="scss">
.departments {
  padding: 30px;
  .main {
    width: 80%;
    margin: 30px auto 0;
  }
}
</style>
